package com.kongwg1.dao;

import com.kongwg1.dao.bean.Customer;

import java.sql.Connection;
import java.sql.Date;
import java.util.List;

/**
 * @author kongwg
 * @create 2021-01-09 16:16
 */
public class CustomerDAOImpl extends BaseDAO implements CustomerDAO {

    @Override
    public void insert(Connection conn, Customer cust) {
        String sql = "insert into customers(id,name,email,birth) values(?,?,?,?)";
        update(conn, sql, cust.getId(), cust.getName(), cust.getEmail(), cust.getBirth());
    }

    @Override
    public void deleteById(Connection conn, int id) {
        String sql = "delete from customers where id = ?";
        update(conn, sql, id);
    }

    @Override
    public void update(Connection conn, Customer cust) {
        String sql = "update customers set  name = ? ,email = ? , birth = ? where id = ?";
        update(conn, sql, cust.getName(),cust.getEmail(),cust.getBirth(),cust.getId());
    }

    @Override
    public Customer getCustomerById(Connection conn, int id) {
        String sql = "select id, name, email, birth from customers where id = ?";
        Customer instance = getInstance(conn,Customer.class, sql, id);
        return instance;
    }

    @Override
    public List<Customer> getAll(Connection conn) {
        String sql = "select id ,name , email, birth from customers";
        List<Customer> forList = getForList(conn, Customer.class, sql);
        return forList;
    }

    @Override
    public long getCount(Connection conn) {
        String sql = "select count(id) from customers";
        Object value = getValue(conn, sql);
        return (long) value;
    }

    @Override
    public Date getMaxBirth(Connection conn) {
        String sql = "select max(birth) from customers";
        Object value = getValue(conn, sql);
        return (Date) value;
    }
}
